package DAO;

import javabean.Customer;

import java.sql.*;

public class CustomerDAO implements ICustomerDAO {
    @Override
    public boolean findLogin(Customer customer) throws Exception {
        boolean flag = false;
        Connection conn = ConnectManager.getConnection();
        Statement stmt = conn.createStatement();
        // 检测用户名和密码是否正确
        String sql = "select * from customer where CustomerID=? and Password=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        String inputID = customer.getCustomerID();
        String inputPassword = customer.getPassword();
        ps.setString(1, inputID);
        ps.setString(2, inputPassword);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            System.out.println("登录成功");
            flag = true;
        } else {
            System.out.println("登录失败");
        }
        ConnectManager.closeConnection(conn, stmt, rs);
        ps.close();
        return flag;
    }


    public boolean doCreate(Customer customer) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean flag = false;

        try {
            conn = ConnectManager.getConnection();
            conn.setAutoCommit(false); // 关闭自动提交

            // 检测用户名是否已经存在
            String sql = "select * from customer where CustomerID=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, customer.getCustomerID());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                System.out.println("用户名已存在");
                return false;
            }

            // 注册新用户
            sql = "insert into customer(CustomerID, CustomerName, Password, CustomerPhone) values(?, ?, ?, ?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1, customer.getCustomerID());
            ps.setString(2, customer.getCustomerName());
            ps.setString(3, customer.getPassword());
            ps.setString(4, customer.getCustomerPhone());
            int result = ps.executeUpdate();
            if (result > 0) {
                System.out.println("注册成功");
                flag = true;
            } else {
                System.out.println("注册失败");
                flag = false;
            }

            conn.commit(); // 提交事务
        } catch (SQLException e) {
            if (conn != null) {
                conn.rollback(); // 回滚事务
            }
            e.printStackTrace();
            flag = false;
        } finally {
            ConnectManager.closeConnection(conn, ps, null);
        }

        return flag;
    }


    public boolean RevisionCustomer(Customer customer) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean flag = false;

        try {
            conn = ConnectManager.getConnection();
            String sql = "UPDATE customer SET CustomerName=?, CustomerPhone=?, CustomerAddress=? WHERE CustomerID=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, customer.getCustomerName());
            ps.setString(2, customer.getCustomerPhone());
            ps.setString(3, customer.getCustomerAddress());
            ps.setString(4, customer.getCustomerID());
            int rows = ps.executeUpdate();
            if (rows > 0) {
                System.out.println("修改成功");
                flag = true;
            } else {
                System.out.println("修改失败");
                flag = false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(conn, ps, null);
        }

        return flag;
    }

    public boolean updatePassword(Customer customer, String newPassword) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean flag = false;

        try {
            conn = ConnectManager.getConnection();
            conn.setAutoCommit(false); // 关闭自动提交

            // 检查旧密码是否正确
            String sql = "select * from customer where CustomerID=? and Password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, customer.getCustomerID());
            ps.setString(2, customer.getPassword());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                // 更新密码
                sql = "update customer set Password=? where CustomerID=?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, newPassword);
                ps.setString(2, customer.getCustomerID());
                int rows = ps.executeUpdate();
                if (rows > 0) {
                    System.out.println("密码修改成功");
                    flag = true;
                } else {
                    System.out.println("密码修改失败");
                    flag = false;
                }
            } else {
                System.out.println("旧密码不正确");
                flag = false;
            }

            conn.commit(); // 提交事务
        } catch (SQLException e) {
            if (conn != null) {
                conn.rollback(); // 回滚事务
            }
            e.printStackTrace();
            flag = false;
        } finally {
            ConnectManager.closeConnection(conn, ps, null);
        }

        return flag;
    }


    public boolean CancellationID(Customer customer) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean flag = false;
        try {
            conn = ConnectManager.getConnection();
            conn.setAutoCommit(false); // 关闭自动提交

            // 检查用户名和密码是否正确
            String sql = "select * from customer where CustomerID=? and Password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, customer.getCustomerID());
            ps.setString(2, customer.getPassword());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                // 删除用户
                sql = "delete from customer where CustomerID=?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, customer.getCustomerID());
                int rows = ps.executeUpdate();
                if (rows > 0) {
                    System.out.println("注销成功");
                    flag = true;
                } else {
                    System.out.println("注销失败");
                    flag = false;
                }
            } else {
                System.out.println("用户名或密码不正确");
                flag = false;
            }

            conn.commit(); // 提交事务
        } catch (SQLException e) {
            if (conn != null) {
                conn.rollback(); // 回滚事务
            }
            e.printStackTrace();
            flag = false;
        } finally {
            ConnectManager.closeConnection(conn, ps, null);
        }

        return flag;
    }

    public boolean ResetPassword(Customer customer, String newPassword) throws SQLException {
        Connection conn = null;
        PreparedStatement ps = null;
        boolean flag = false;

        try {
            conn = ConnectManager.getConnection();
            conn.setAutoCommit(false); // 关闭自动提交

            // 检查用户名和手机号是否匹配
            String sql = "select * from customer where CustomerID=? and CustomerPhone=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, customer.getCustomerID());
            ps.setString(2, customer.getCustomerPhone());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                // 更新密码
                sql = "update customer set Password=? where CustomerID=?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, newPassword);
                ps.setString(2, customer.getCustomerID());
                int rows = ps.executeUpdate();
                if (rows > 0) {
                    System.out.println("密码重置成功");
                    flag = true;
                } else {
                    System.out.println("密码重置失败");
                    flag = false;
                }
            } else {
                System.out.println("用户名和手机号不匹配");
                flag = false;
            }

            conn.commit(); // 提交事务
        } catch (SQLException e) {
            if (conn != null) {
                conn.rollback(); // 回滚事务
            }
            e.printStackTrace();
            flag = false;
        } finally {
            ConnectManager.closeConnection(conn, ps, null);
        }

        return flag;
    }


    public Customer SearchCustomer(String customerID) throws SQLException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Customer customer = null;

        try {
            conn = ConnectManager.getConnection();
            String sql = "SELECT * FROM customer WHERE customerID = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, customerID);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                String name = rs.getString("customerName");
                String phone = rs.getString("customerPhone");
                String address = rs.getString("customerAddress");
                double balance = rs.getDouble("customerBalance");
                String password = rs.getString("password");
                customer = new Customer(customerID, name, phone, address, balance, password);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw e;
        } finally {
            ConnectManager.closeConnection(conn, pstmt, rs);
        }
        return customer;
    }

}

//    public boolean doCreate(Customer customer) throws SQLException {
//        Connection conn = null;
//        PreparedStatement ps = null;
//        ResultSet rs = null;
//        Statement stmt = null;
//        boolean flag = false;
//        try {
//            conn = ConnectManager.getConnection();
//            stmt = conn.createStatement();
//            String inputID = customer.getCustomerID();
//            // 检测用户名是否已经存在
//            String sql = "select * from customer where CustomerID=?";
//            ps = conn.prepareStatement(sql);
//            ps.setString(1, inputID);
//            rs = ps.executeQuery();
//            if (rs.next()) {
//                System.out.println("用户名已存在");
//                return false;
//            }
//
//            // 注册用户
//            String inputName = customer.getCustomerName();
//            String inputPassword = customer.getPassword();
//            String inputPhone = customer.getCustomerPhone();
//            sql = "insert into customer(CustomerID, CustomerName, Password, CustomerPhone) values(?, ?, ?, ?)";
//            ps = conn.prepareStatement(sql);
//            ps.setString(1, inputID);
//            ps.setString(2, inputName);
//            ps.setString(3, inputPassword);
//            ps.setString(4, inputPhone);
//            int result = ps.executeUpdate();
//            if (result > 0) {
//                System.out.println("注册成功");
//                flag = true;
//            } else {
//                System.out.println("注册失败");
//                flag = false;
//            }
//            return flag;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return false;
//        } finally {
//            ConnectManager.closeConnection(conn, stmt, rs);
//            if (ps != null) {
//                ps.close();
//            }
//        }
//    }
